home *** CD-ROM | disk | FTP | other *** search
- PAGE ,132
- TITLE PMODE
-
- COMMENT @
-
- These procedures allow a protected mode program to avoid certain
- protection exceptions.
-
- NOTES: 1. These procedures are intended to run in protected mode
- on a 286 or 386-based computer. Any attempt to execute
- them in real mode, or on an 8088-based PC will most likely
- result in a system crash!
-
- 2. This file was assembled using Microsoft's MASM Version 4.0
-
- @
-
- .286p ;enable protected mode instructions
-
- CODE SEGMENT PUBLIC 'CODE'
- ASSUME CS:CODE
-
- ;------------------------------------------------------------------------------
- ; check_segment_limit: Determine if an offset is usable within a segment
- ; INPUT: BX - selector for the segment
- ; CX - offset to check
- ; OUTPUT: CF: 0 - selector:offset is OK
- ; 1 - exception will occur if selector:offset used
- ;------------------------------------------------------------------------------
- check_segment_limit PROC
- PUBLIC check_segment_limit
-
- PUSH DX
- LSL DX,BX ;obtain the segment's limit
- JNZ exception_return ;exit if bad selector
- CMP CX,DX ;compare offset and limit
- JA exception_return ;jump if offset above limit
- CLC ;selector:offset OK
- JMP SHORT csl_exit ;return to caller
- exception_return:
- STC ;bad selector or offset
- csl_exit:
- POP DX
- RET
-
- check_segment_limit ENDP
-
-
-
- ;------------------------------------------------------------------------------
- ; check_sensitive: Determine if we can execute sensitive instructions
- ; INPUT: None
- ; OUTPUT: CF: 0 - sensitive instructions OK at current privilege
- ; 1 - exception will occur if we attempt a sensitive
- ; instruction
- ;------------------------------------------------------------------------------
- check_sensitive PROC
- PUBLIC check_sensitive
-
- PUSH AX
- PUSH BX
-
- PUSHF ;save flags (IOPL) on stack
- POP AX ;copy flags to AX
- AND AX,3000H ;mask all but IOPL
- SHR AX,12 ;right-justify IOPL
- MOV BX,CS ;CPL resides in CS
- AND BX,3 ;mask all but CPL
- CMP BX,AX ;compare CPL and IOPL
- JA no_sensitive ;jump if CPL > IOPL
- CLC ;sensitive instructions OK
- JMP SHORT cs_exit
- no_sensitive:
- STC ;exception occurs on sensitive
- cs_exit:
- POP BX
- POP AX
- RET
-
- check_sensitive ENDP
-
- ;------------------------------------------------------------------------------
- ; check_privileged: Determine if we can execute privileged instructions
- ; INPUT: None
- ; OUTPUT: CF: 0 - privileged instructions OK at current privilege
- ; 1 - exception will occur if we attempt a privileged
- ; instruction
- ;------------------------------------------------------------------------------
- check_privileged PROC
- PUBLIC check_privileged
-
- PUSH AX
- MOV AX,CS ;CPL resides in CS
- AND AX,3 ;mask all but CPL
- JNZ no_privileged ;jump if CPL <> 0
- CLC ;privileged instructions OK
- JMP SHORT cp_exit
- no_privileged:
- STC ;exception occurs on privileged
- cp_exit:
- POP AX
- RET
-
- check_privileged ENDP
-
- CODE ENDS
- END
- no_pr